﻿package com.makinolo.rlvforge
{
	
public class Utils
{
	public static function parseUTCDate( str : String ) : Date 
	{		
    	var matches : Array = str.match(/(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)(,*(\d*)Z)*/);

	    var d : Date = new Date();

    	d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));
	    d.setUTCHours(int(matches[4]), int(matches[5]), int(matches[6]), int(matches[7]));

    	return d;
	}
	
	public static function formatTime(seconds:Number, long:Boolean = true):String
	{
		var minutes:Number = Math.floor(seconds/60);
		var remainingSec:Number = seconds % 60;
		var remainingMinutes:Number = minutes % 60;
		var hours:Number = Math.floor(minutes/60);
		var floatSeconds:Number = Math.floor((remainingSec - Math.floor(remainingSec))*100);
		remainingSec = Math.floor(remainingSec);

		//Edit the formatting below for a custom format
		if (long)
			return (getTwoDigits(hours)+":"+getTwoDigits(remainingMinutes)+":"+getTwoDigits(remainingSec)+"."+getTwoDigits(floatSeconds));
		else
			return (hours+":"+getTwoDigits(remainingMinutes)+":"+getTwoDigits(remainingSec));			
	}

	public static function getTwoDigits(number:Number):String
	{
		if(number < 10){return("0"+number);} 
		else {return number+"";}
	}
	
	public static function formatDate(d:Date):String
	{
		return getTwoDigits(d.fullYear)+"-"+
				getTwoDigits(d.month)+"-"+
				getTwoDigits(d.date)+"T"+
				getTwoDigits(d.hours)+":"+
				getTwoDigits(d.minutes)+":"+
				getTwoDigits(d.seconds)+"."+
				d.milliseconds+"Z";
	}	
	
	public static function formatDateMillis(t:Number):String
	{
		var d:Date = new Date(t);
		return getTwoDigits(d.fullYear)+"-"+
				getTwoDigits(d.month)+"-"+
				getTwoDigits(d.date)+"T"+
				getTwoDigits(d.hours)+":"+
				getTwoDigits(d.minutes)+":"+
				getTwoDigits(d.seconds)+","+
				Math.round(((t/1000) - Math.floor(t/1000))*1000)+"Z";
	}
	
	public static function formatNumber(number:Number, decimals:int=0):String
	{	
		return number.toFixed(decimals);
	}	

}
}